package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.ExperimentCommentMapper;
import com.brillilab.domain.po.experiments.ExperimentComment;
import com.brillilab.domain.vo.experiments.ExperimentCommentVo;
import com.brillilab.service.core.experiments.IExperimentCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author linzhikang
 * @description
 * @date 2019-09-10 10:26
 */
@Service
@Transactional
public class ExperimentCommentServiceImpl implements IExperimentCommentService {

    @Resource
    private ExperimentCommentMapper experimentCommentMapper;

    @Override
    public ExperimentComment addComment(ExperimentComment comment) {
        Assert.isTrue(comment != null, "请求参数缺失!");
        int insert = experimentCommentMapper.insert(comment);
        org.springframework.util.Assert.isTrue(insert == 1, "操作失败！");
        return comment;
    }

    @Override
    public boolean deleteComment(Long commentId) {
        Assert.isTrue(commentId != null, "请求参数缺失!");
        return experimentCommentMapper.deleteById(commentId) == 1;
    }

    @Override
    public ExperimentComment selectById(Long commentId) {
        Assert.isTrue(commentId != null, "请求参数缺失!");
        return experimentCommentMapper.selectById(commentId);
    }

    @Override
    public List<ExperimentCommentVo> selectList(Long experimentId,Integer type) {
        Assert.isTrue(experimentId != null, "请求参数缺失!");
        return experimentCommentMapper.selectVoList(experimentId,type);

    }

    @Override
    public List<ExperimentCommentVo> selectCommentList(List<Long> experimentIds) {
        Assert.isTrue(experimentIds != null, "请求参数缺失!");
        return experimentCommentMapper.selectCommentList(experimentIds);
    }

    @Override
	public int getCount(Long experimentId,Integer isRead) {
		LambdaQueryWrapper<ExperimentComment> queryWrapper = new LambdaQueryWrapper<ExperimentComment>();
		queryWrapper.eq(ExperimentComment::getExperimentId,experimentId);
		if(isRead!=null)
			queryWrapper.eq(ExperimentComment::getIsRead,isRead);
		return experimentCommentMapper.selectCount(queryWrapper);
	}

    @Override
    public int commentNum(Long experimentId, Integer type) {
        LambdaQueryWrapper<ExperimentComment> queryWrapper = new LambdaQueryWrapper<ExperimentComment>();
        queryWrapper.eq(ExperimentComment::getExperimentId,experimentId);
        if(type!=null) {
        	queryWrapper.eq(ExperimentComment::getType,type);
        }
        return experimentCommentMapper.selectCount(queryWrapper);
    }

    @Override
    public void setReadState(Long experimentId) {
        experimentCommentMapper.setReadState(experimentId);
    }

	@Override
	public List<ExperimentComment> selectAllList() {
		LambdaQueryWrapper<ExperimentComment> queryWrapper = new LambdaQueryWrapper<ExperimentComment>();
		return experimentCommentMapper.selectList(queryWrapper);
	}
}
